package com.yhgarden.bbs.utils.captcha;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author caimb
 * @date Created at 2018/11/8 15:14
 * @modefier
 */
public class CaptchaExtend {
    private final static String captchaSessionKey = null == null ? "KAPTCHA_SESSION_KEY" : "";

    //使用方法
    public static void captcha(HttpServletRequest req, HttpServletResponse response) throws IOException {
        HttpSession session = req.getSession();
        // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        CaptchaProducer captcha = new CaptchaProducer();
        captcha.create();
        session.setAttribute( captchaSessionKey, captcha.getCode() );
        captcha.write(response.getOutputStream());
    }
    //使用方法
    public static void captcha(int width, int height,
                               HttpServletRequest req, HttpServletResponse response) throws IOException {
        HttpSession session = req.getSession();
        // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        if(width > 150 || height > 50) {
            return;
        }
        CaptchaProducer captcha = new CaptchaProducer();
        captcha.create( width, height );
        session.setAttribute( captchaSessionKey, captcha.getCode() );
        captcha.write(response.getOutputStream());
    }

    public static void captcha(int width, int height, int count,
                               HttpServletRequest req, HttpServletResponse response) throws IOException {
        HttpSession session = req.getSession();
        // 设置响应的类型格式为图片格式
        response.setContentType( "image/jpeg" );
        //禁止图像缓存。
        response.setHeader( "Pragma", "no-cache" );
        response.setHeader( "Cache-Control", "no-cache" );
        response.setDateHeader( "Expires", 0 );

        if ( width > 150 || height > 50 ) {
            return;
        }
        if ( count > 6 ) {
            return;
        }
        CaptchaProducer captcha = new CaptchaProducer();
        captcha.create( width, height, count );
        session.setAttribute( captchaSessionKey, captcha.getCode() );
        captcha.write(response.getOutputStream());
    }

    public static String getGeneratedKey(HttpServletRequest req) {
        HttpSession session = req.getSession();
        Object capObj = session.getAttribute( captchaSessionKey );
        return capObj != null ? capObj.toString() : null;
    }

    public static boolean validateCaptcha(HttpServletRequest request, String captchaFromClient) {
        String captchaKeyInSession = getGeneratedKey(request);
        if (captchaKeyInSession == null || captchaKeyInSession.isEmpty()) {
            return false;
        }
        if (captchaFromClient == null || captchaFromClient.isEmpty()) {
            return false;
        }
        if (!captchaFromClient.equalsIgnoreCase(captchaKeyInSession)) {
            return false;
        }
        return true;
    }
}
